from  selenium import webdriver
from selenium.common import WebDriverException, InvalidArgumentException, NoSuchElementException
from selenium.webdriver.common.by import    By
import time


class Base:
    def __init__(self,driver):
        if driver =='chrome':
            self.driver =webdriver.Chrome()
        elif driver =='firefox':
            self.driver=webdriver.Firefox()
        elif driver =='edge':
            self.driver=webdriver.Edge()
        else:
            print('输入网址无效')
            self.driver.maximize_window()


    def input_url(self,url):
        try:
            self.driver.get(url)
        except (WebDriverException,InvalidArgumentException):
            print('输入无效的网址')

    def elelocate(self,locator):
        try:
            ele=self.driver.find_element(*locator)
            return ele
        except (NoSuchElementException):
            print('没有定位到元素')

    def click(self,locator):
            element=self.elelocate(locator)
            if element is not None:
                element.click()
            else:
                print('找不到元素')

    def send_keys(self,locator,msg):
        sd=self.elelocate(locator)
        if sd is not None:
            sd.send_keys(msg)
        else:
            print('找不到输入框')

    def  get_attribute(self,locator,value):
        ga=self.elelocate(locator)
        if ga is not None:
            ga.get_attribute(value)
            return ga
        else:
            print('找不到属性')


    def get_text(self,locator):
        gt = self.elelocate(locator)
        if gt is not None:
            print(gt.text)
        else:
            print('找不到属性')

    def clear(self,locator):
        cl=self.elelocate(locator)
        if cl is not None:
            cl.clear()
        else:
            print('找不到输入框')

    def quit(self,seconds=3):
        time.sleep(seconds)
        self.driver.quit()

    def refresh(self):
        time.sleep(2)
        self.driver.refresh()
        time.sleep(2)

    def forward(self):
        time.sleep(2)
        self.driver.forward()

    def back(self):
        time.sleep(2)
        self.driver.back()



if __name__ == '__main__':
    base= Base('chrome')
    base.input_url('https://ww.baidu.com')
    base.elelocate((By.ID,'kw'))
    base.get_attribute((By.ID,'su'),'id')
    # base.send_keys((By.ID,'kw'),'啦啦啦啦')
    base.get_text((By.LINK_TEXT,'新闻'))
    base.click((By.ID,'su'))
    base.quit()




























